package br.com.gp.fabrica;

import java.util.ArrayList;
import java.util.List;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import br.com.gp.anotation.Consulta;
import br.com.gp.anotation.SaveOrUpdate;
import br.com.gp.aop.AdivisorConsultaAfter;
import br.com.gp.aop.AdivisorConsultaBefore;
import br.com.gp.aop.AdivisorSaveOrUpdateAfter;
import br.com.gp.aop.AdivisorSaveOrUpdateBefore;

public class FabricaInterceptPoint {
	public List<Advisor> getInterceptPointConsulta(){
		List<Advisor> lista = new ArrayList<Advisor>();
		AnnotationMatchingPointcut pontoConsulta = getPointCut(Consulta.class);
		Advice consultaAfter = new AdivisorConsultaAfter();
        Advice consultaBefore = new AdivisorConsultaBefore();
        Advisor advisorConsultaAfter = new DefaultPointcutAdvisor(pontoConsulta, consultaAfter);
        Advisor advisorConsultaBefore = new DefaultPointcutAdvisor(pontoConsulta, consultaBefore);
        lista.add(advisorConsultaAfter);
        lista.add(advisorConsultaBefore);
		return lista;
	}
	public List<Advisor> getInterceptPointSaveOrUpdate(){
		List<Advisor> lista = new ArrayList<Advisor>();
		AnnotationMatchingPointcut pontoSaveOrUpdate = getPointCut(SaveOrUpdate.class);
		Advice consultaAfter = new AdivisorSaveOrUpdateAfter();
        Advice consultaBefore = new AdivisorSaveOrUpdateBefore();
        Advisor advisorSaveOrUpdateAfter = new DefaultPointcutAdvisor(pontoSaveOrUpdate, consultaAfter);
        Advisor advisorSaveOrUpdateBefore = new DefaultPointcutAdvisor(pontoSaveOrUpdate, consultaBefore);
        lista.add(advisorSaveOrUpdateAfter);
        lista.add(advisorSaveOrUpdateBefore);
		return lista;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private  AnnotationMatchingPointcut getPointCut(Class cls){
		return new AnnotationMatchingPointcut(null, cls);
	}
}
